﻿using System;
using System.Collections.Generic;
using System.Text;
using WordService.Business.IBLL;
using WordService.Data;
using WordService.Data.IDAL;
using WordService.Model;

namespace WordService.Business.BLL
{
    public sealed class AuthorBLL:IAuthorBLL
    {
        private IAuthorDAL authorDAL = DALManager.CreateAuthorDAL();

        public Model.Author CreateAuthor(Model.Author author)
        {
            Decimal rightRate = Decimal.Parse(author.RightRate.ToString());
            author.ID = authorDAL.InsertAuthor(author.Name, author.Sequence, author.Article.ID, rightRate,author.TotalCount);
            return author;
        }
        /// <summary>
        /// 是否进行检测重复项地插入方式
        /// </summary>
        /// <param name="authors">为False的时候需要检测重复项</param>
        /// <param name="isSame">如果为True就不检测重复项</param>
        /// <returns></returns>
        public Boolean CreateAuthors(IList<Model.Author> authors, Boolean isSame)
        {
            Boolean result = false;
            try{
                if( isSame)
                {
                    foreach( Author author in authors)
                    {
                        Decimal rightRate = Decimal.Parse(author.RightRate.ToString());
                        authorDAL.InsertAuthor(author.Name, author.Sequence, author.Article.ID, rightRate,author.TotalCount);
                    }
                }else{
                    foreach( Author author in authors)
                    {
                        Decimal rightRate = Decimal.Parse(author.RightRate.ToString());
                        authorDAL.SaveAuthor(author.Name, author.Sequence, author.Article.ID, rightRate,author.TotalCount);
                    }
                }
                result = true;
            }catch(Exception){
                result = false;
            }
            return result;
        }

        public Boolean CheckAuthor(Model.Author author)
        {
            throw new NotImplementedException();
        }

        public Boolean ModifyAuthor(Model.Author author)
        {
            Decimal rightRate = Decimal.Parse(author.RightRate.ToString());
            Int64 result = authorDAL.UpdateAuthor(author.Name, author.Sequence, author.Article.ID, rightRate, author.TotalCount, author.ID);
            if (result == 0)
            {
                return false;
            }
            else
            {
                return true;
            }
        }

        public IList<Author> QueryByCondition(string keywordName, string insName, long frequency, string year)
        {
            return authorDAL.QueryByCondition(keywordName, insName, frequency, year);
        }

        public IList<Author> QueryAllAuthors()
        {
            return authorDAL.QueryByCondition(null, null, 0, null);
        }
    }
}
